<?php

class AdminStoriesController extends AdminAppController {
	
	public $name = 'Stories';
	
	public $uses = array('Story');
	
	public function index() {
		$this->Story->recursive = 0;
		$this->paginate = array (
			'order' => array('Story.id' => 'desc')
		);
		$this->set('stories', $this->paginate());
	}
	
	public function create() {
		if( $this->request->is('post') ) {
			$this->Story->create();
			if( $this->Story->save($this->request->data) ) {
				$this->setUserInfo(__d('admin', 'The Story has been created.'), 'success');
				$this->redirect(array('action' => 'index'));
			}
			else {
				$this->log('Error creating Story: ' . print_r($this->Story->validationErrors, true));
				$this->setUserInfo(__d('admin', 'The Story could not be saved. Please, try again.'), 'error');
			}
		}
		$this->set('authors', $this->Story->Author->find('list'));
		$this->set('categories', $this->Story->Category->generateTreeList(null, null, null, "&nbsp;&nbsp;&nbsp;"));
	}
	
	public function edit($id) {
		if( $this->request->is('put') ) {
			if( $this->Story->save($this->request->data) ) {
				$this->setUserInfo(__d('admin', 'The Story has been updated.'), 'success');
				$this->redirect(array('action' => 'index'));
			}
			else {
				$this->log('Error updating Story: ' . print_r($this->Story->validationErrors, true));
				$this->setUserInfo(__d('admin', 'The Story could not be saved. Please, try again.'), 'error');
			}
		}
		$this->request->data = $this->Story->read(null, $id);
		$this->set('authors', $this->Story->Author->find('list'));
		$this->render('create');
	}
	
}